<template>
	<view class="container">
		<!-- 颈部查询 -->
		<view class="navbar">
			<view class="search-item" v-for="(item,index) in formList">
				<view class="label">
					{{ item.label}}
				</view>
				<u-input v-model="form[item.prop]" :placeholder="item.placeholder||'请输入'" border></u-input>
			</view>
			<view class="button-part">
				<u-button type="primary" size="mini" @click="dataSearch">查询</u-button>
			</view>

		</view>
		<!-- 数据表格 -->
		<view class="content">
			<view class="table-head">
				<u-table>
					<u-tr>
						<u-th v-for="(col,colIndex) in tableDatas.columns" :key="colIndex" :width="col.width">
							{{col.label}}
						</u-th>
					</u-tr>
				</u-table>
			</view>
			<view class="table-body">
				<scroll-view scroll-y="true" style="height: 100%;" @scrolltolower="scrolltolower()"
					:lower-threshold="3">
					<u-table font-size="24">
						<u-tr v-for="(row,rowIndex) in tableDatas.datas" :key="rowIndex">
							<u-td v-for="(col,colIndex) in tableDatas.columns" :key="colIndex" :width="col.width"
								:class="`col-${colIndex} ${col.prop==='edit'?'col-edit':''}`"
								@click.native="clickRow(row,col)">
								{{ col.prop==='edit'?'进入发料': col.prop==='CreatedOn'?formatDate(row[col.prop],'yyyy/mm/dd'):row[col.prop]}}
							</u-td>
						</u-tr>
						<u-loadmore :status="tableDatas.loadmoreVisible" @loadmore="loadMore()" color="#3e82f9" />
					</u-table>
				</scroll-view>
			</view>
		</view>
		<!-- 底部数量统计 -->
		<!-- <view class="footer">
			总计：{{ this.tableDatas.count}}条
		</view> -->
	</view>
</template>

<script>
	import myTab from '@/components/myTabs/index.vue'
	export default {
		components: {
			myTab
		},
		data() {
			return {
				formList: [{
						label: '仓库：',
						prop: 'WarehouseName1',
						placeholder: '请输入'
					},
					{
						label: '货架编码：',
						prop: 'Code',
						placeholder: '请输入'
					}
				],
				form: {
					page: 1,
					rows: 50,
				},
				tableDatas: {
					dicID: 158,
					datas: [],
					columns: [{
							prop: 'WarehouseName1',
							label: '仓库名称',
							width: '30%'
						},
						{
							prop: 'Code',
							label: '货架编码',
							width: '25%'
						},
						{
							prop: 'WarehouseName',
							label: '货架名称',
							width: '27%',
							type: 'numberbox'
						},
						{
							prop: 'ShelvesCount',
							label: '储位数量',
							width: '18%'
						},

					],
					formData: {},
					loading: false,
					loadmoreVisible: 'loadmore',
					loadText: '滚动或点击加载更多',
					count: 0,
					selectAll: false
				},
			}
		},
		onLoad() {
			this.formList.forEach(val => {
				this.$set(this.form, val.prop, val.value || null)
			})
			this.getApsData()
		},
		methods: {
			formatDate(val, format = 'yyyy-mm-dd') {
				return this.$u.timeFormat(new Date(val), format)
			},
			dataSearch() {
				this.form.page = 1
				this.getApsData()
			},
			// 获取数据
			async getApsData(remark = 0) {
				const condition = {
					dicID: this.tableDatas.dicID,
				}
				Object.assign(condition, this.form)
				this.tableDatas.loading = true
				if (this.form.page === 1) {
					this.tableDatas.datas.length = 0
				}
				this.setLoadmore('loading')
				const res = await this.$request.post({
					url: '/APSAPI/APSData',
					data: condition
				}).catch(err => err)
				this.tableDatas.loading = false
				if (!res) return
				const {
					result,
					msg,
					data,
					count
				} = res
				if (!result) return uni.showToast({
					title: msg,
					icon: 'error'
				})
				if (res.result) {
					this.$set(this.tableDatas, 'count', count)
					if (!res.data.length) {
						this.setLoadmore('nomore')
					} else {
						this.tableDatas.datas.push(...res.data)
						this.setLoadmore('loadmore')
					}
				} else {
					return Promise.reject()
				}
			},
			// 加载更多
			loadMore() {
				if (this.tableDatas.loadmoreVisible === 'nomore') return
				this.form.page++
				this.getApsData()
			},
			//配置加载更多
			setLoadmore(status = 'loadmore') {
				const textMenu = {
					loadmore: '滑动或点击加载更多',
					loading: '加载中...',
					nomore: '已无更多数据',
				}
				this.$set(this.tableDatas, 'loadmoreVisible', status)
				this.$set(this.tableDatas, 'loadmoreText', textMenu[status])
			},
			// 表格滚动到底部
			scrolltolower() {
				this.loadMore()
			},
			clickRow(row, col) {
				if (col.prop !== 'Code' && col.prop !== 'WarehouseName') return
				this.$u.route({
					url: '/pages/Isgo/warehouse/shelvesSetting',
					params: {
						row: JSON.stringify(row) // 转成字符串传递
					}
				});
			},
		}
	}
</script>

<style scoped lang="scss">
	* {
		box-sizing: border-box;
	}

	.container {
		width: 100%;
		height: 100vh;
		overflow: hidden;

		.header {
			height: 81rpx;
			// border-bottom: 1px solid $uni-border-color;
		}

		.navbar {
			padding: 0 10rpx;
			height: 80rpx;
			display: flex;
			flex-wrap: nowrap;

			.search-item {
				flex-grow: 1;
				padding-right: 20rpx;
				display: flex;
				flex-wrap: nowrap;
				align-items: center;
			}

			.button-part {
				width: 150rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}

		.content {
			height: calc(100vh - 80rpx);
			width: 100%;
			// flex: 1;
			display: flex;
			flex-direction: column;
			padding: 10rpx 10rpx 20rpx;
			overflow: hidden;

			.table-head {}

			.table-body {
				flex-grow: 1;
				width: 100%;
				overflow: hidden;

				uni-scroll-view {
					height: 100%;
				}

				.u-table {
					width: 100%;
					overflow-y: auto;
					overflow-x: hidden;
				}
			}

			.u-td,
			.u-th {
				white-space: nowrap !important;
				overflow: hidden !important;
				text-overflow: ellipsis !important;
				min-width: 0 !important;
				/* 关键属性 */
				flex-shrink: 0 !important;
				/* 防止flex容器压缩 */
			}

			.u-td {
				height: 60rpx;
			}

			.col-1 {
				white-space: pre-wrap !important;
			}

			.col-0 {
				// font-size: 18rpx !important;
				white-space: pre-wrap !important;
				font-weight: bold;
			}

			.col-4 {
				// color: red !important;
			}

			.col-2,
			.col-1 {
				color: $uni-color-primary !important;
				text-decoration-line: underline;
			}

		}

		.footer {
			height: 60rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			padding-right: 20rpx;
		}
	}
</style>